package com.jason.practice.service.impl;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jason.practice.mapper.CitizenLoanInfoMapper;
import com.jason.practice.model.CitizenLoanInfo;
import com.jason.practice.response.Response;
import com.jason.practice.service.CitizenLoanInfoService;
import com.jason.practice.utils.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiayb_9169@126.com
 * @since 2022-10-23
 */
@Service
public class CitizenLoanInfoServiceImpl extends ServiceImpl<CitizenLoanInfoMapper, CitizenLoanInfo> implements CitizenLoanInfoService {
    @Override
    public Response uploadExcel(File file) {
        List<CitizenLoanInfo> t0079JList = new ArrayList<>();
        try {
            ImportParams im = new ImportParams();
            im.setTitleRows(3);
            im.setHeadRows(1);
            im.setStartSheetIndex(0);
            im.setReadRows(7);
            t0079JList = ExcelUtil.parseExcel(file, CitizenLoanInfo.class, im);
            int creditLoanNum = 0;
            BigDecimal loanBalance = new BigDecimal(0.00);
            BigDecimal creditLine = new BigDecimal(0.00);
            BigDecimal accGrantLoan = new BigDecimal(0.00);
            int workNum = 0;
            for (CitizenLoanInfo t0079J : t0079JList) {
                t0079J.setExcelName(file.getName());
                t0079J.setSheetName("支持新市民信贷产品情况统计表");
                if (!StringUtils.isBlank(t0079J.getCreditLoanNum())) {
                    creditLoanNum += NumberUtils.toInt(t0079J.getCreditLoanNum());
                }
                if (!StringUtils.isBlank(t0079J.getLoanBalance())) {
                    BigDecimal loanBalanceDecimal = new BigDecimal(t0079J.getLoanBalance());
                    loanBalance = loanBalance.add(new BigDecimal(loanBalanceDecimal.toString()));
                }
                if (!StringUtils.isBlank(t0079J.getCreditLine())) {
                    BigDecimal bd = new BigDecimal(t0079J.getCreditLine());
                    creditLine = creditLine.add(new BigDecimal(bd.toString()));
                }
                if (!StringUtils.isBlank(t0079J.getAccGrantLoan())) {
                    BigDecimal bd = new BigDecimal(t0079J.getAccGrantLoan());
                    accGrantLoan = accGrantLoan.add(new BigDecimal(bd.toString()));
                }
                if (!StringUtils.isBlank(t0079J.getWorkNum())) {
                    workNum += NumberUtils.toInt(t0079J.getWorkNum());
                }
            }
            CitizenLoanInfo total = new CitizenLoanInfo();
            total.setCreditLoanNum(creditLoanNum+"");
            total.setLoanBalance(loanBalance.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"");
            total.setCreditLine(creditLine.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"");
            total.setAccGrantLoan(accGrantLoan.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"");
            total.setWorkNum(workNum+"");
            ExcelUtil.editExcel(file, total);
//            saveBatch(t0079JList);
        } catch (Exception e) {
            log.error("BaseCommonServiceImpl error:", e);
        }
        return Response.ok(t0079JList);
    }
}
